using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using Customs.Components.KDT.ThanhKhoan.Info;

namespace Customs.Components.KDT.ThanhKhoan
{
    public class BKNPLHuyBieuTangDangKy :BKNPLHuyBieuTangDangKyInfo
    {
        public bool Load()
        {
            SqlDataReader reader = Select();
            if (reader.Read())
            {
                if (!reader.IsDBNull(reader.GetOrdinal("So_TNDKDT"))) this.SoTiepNhan = reader.GetInt64(reader.GetOrdinal("So_TNDKDT"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ma_HQTN"))) this.MaHaiQuan = reader.GetString(reader.GetOrdinal("Ma_HQTN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Nam_TN"))) this.NamTiepNhan = reader.GetInt16(reader.GetOrdinal("Nam_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ngay_TN"))) this.NgayTiepNhan = reader.GetDateTime(reader.GetOrdinal("Ngay_TN"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ma_DV"))) this.MaDoanhNghiep = reader.GetString(reader.GetOrdinal("Ma_DV"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_TNCT"))) this.So_TNCT = reader.GetInt64(reader.GetOrdinal("So_TNCT"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ngay_TNCT"))) this.Ngay_TNCT = reader.GetDateTime(reader.GetOrdinal("Ngay_TNCT"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXL"))) this.TrangThaiXL = reader.GetInt32(reader.GetOrdinal("TrangThaiXL"));
                if (!reader.IsDBNull(reader.GetOrdinal("So_HSTL"))) this.SoHoSoThanhLy = reader.GetInt64(reader.GetOrdinal("So_HSTL"));
                reader.Close();
                return true;
            }
            reader.Close();
            return false; ;
        }

        public void Load(BKNPLHuyBieuTangDangKyInfo entity)
        {
            this._Ma_DV = entity.MaDoanhNghiep;
            this._Ma_DVKB = entity.Ma_DVKB;
            this._Ma_HQTN = entity.MaHaiQuan;
            this._Nam_TN = entity.NamTiepNhan;
            this._Ngay_TN = entity.NgayTiepNhan;
            this._Ngay_TNCT = entity.Ngay_TNCT;
            this._So_HSTL = entity.SoHoSoThanhLy;
            this._So_TNCT = entity.So_TNCT;
            this._So_TNDKDT = entity.SoTiepNhan;
            this._TrangThaiXL = entity.TrangThaiXL;
            this._collection = entity.NPLCollection;
        }
        public static BKNPLHuyBieuTangDangKyInfo ExportToInfo(long SoTiepNhan, string MaHaiQuan, short NamTiepNhan)
        {
            BKNPLHuyBieuTangDangKy bkDangKy = new BKNPLHuyBieuTangDangKy();
            bkDangKy.SoTiepNhan = SoTiepNhan;
            bkDangKy.MaHaiQuan = MaHaiQuan;
            bkDangKy.NamTiepNhan = NamTiepNhan;
            if (!bkDangKy.Load())
                return null;
            BKNPLHuyBieuTangDangKyInfo entity = new BKNPLHuyBieuTangDangKyInfo();
            bkDangKy.LoadNPLCollection();
            entity.MaDoanhNghiep = bkDangKy.MaDoanhNghiep;
            entity.Ma_DVKB = bkDangKy.Ma_DVKB;
            entity.MaHaiQuan = bkDangKy.MaHaiQuan;
            entity.NamTiepNhan = bkDangKy.NamTiepNhan;
            entity.NgayTiepNhan = bkDangKy.NgayTiepNhan;
            entity.Ngay_TNCT = bkDangKy.Ngay_TNCT;
            entity.SoHoSoThanhLy = bkDangKy.SoHoSoThanhLy;
            entity.So_TNCT = bkDangKy.So_TNCT;
            entity.SoTiepNhan = bkDangKy.SoTiepNhan;
            entity.TrangThaiXL = bkDangKy.TrangThaiXL;
            entity.NPLCollection = bkDangKy.NPLCollection;
            return entity;
        }
        public SqlDataReader Select()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
            string sql = "select * from DTLNPLXH where So_TNDKDT=@So_TNDKDT and Ma_HQTN=@Ma_HQTN and Nam_TN=@Nam_TN";

            SqlParameter[] parameters = new SqlParameter[3];
            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this.SoTiepNhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = this.MaHaiQuan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this.NamTiepNhan;


            return SqlHelper.ExecuteReader(connectionString, CommandType.Text, sql, parameters);
        }

        public int Delete(SqlTransaction transaction)
        {
            string sql = "delete from DTLNPLXH where So_TNDKDT=@So_TNDKDT and Ma_HQTN=@Ma_HQTN and Nam_TN=@Nam_TN";
            SqlParameter[] parameters = new SqlParameter[3];

            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this.SoTiepNhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = this.MaHaiQuan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this.NamTiepNhan;
            return SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, sql, parameters);

        }

        public int Insert(SqlTransaction transaction)
        {
            StringBuilder query = new StringBuilder();
            query.Append("INSERT INTO DTLNPLXH (");
            query.Append("So_TNDKDT, ");
            query.Append("Ma_HQTN, ");
            query.Append("Nam_TN, ");
            query.Append("Ngay_TN, ");
            query.Append("So_TNCT, ");
            query.Append("Ngay_TNCT, ");
            query.Append("MA_DV, ");
            query.Append("Ma_DVKB, ");
            query.Append("TrangThaiXL, ");
            query.Append("AppSource, ");
            query.Append("So_HSTL ) ");
            query.Append("VALUES ( ");

            query.Append("@So_TNDKDT, ");
            query.Append("@Ma_HQTN, ");
            query.Append("@Nam_TN, ");
            query.Append("@Ngay_TN, ");
            query.Append("@So_TNCT, ");
            query.Append("@Ngay_TNCT, ");
            query.Append("@MA_DV, ");
            query.Append("@Ma_DVKB, ");
            query.Append("@TrangThaiXL, ");
            query.Append("@AppSource, ");
            query.Append("@So_HSTL ) ");

            SqlParameter[] parameters = new SqlParameter[11];

            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this.SoTiepNhan;

            parameters[1] = new SqlParameter("@Ma_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = this.MaHaiQuan;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this.NamTiepNhan;

            parameters[3] = new SqlParameter("@Ngay_TN", SqlDbType.DateTime);
            parameters[3].Value = this.NgayTiepNhan;

            parameters[4] = new SqlParameter("@So_TNCT", SqlDbType.BigInt);
            parameters[4].Value = this.So_TNCT;

            parameters[5] = new SqlParameter("@Ngay_TNCT", SqlDbType.DateTime);
            parameters[5].Value = this.Ngay_TNCT;

            parameters[6] = new SqlParameter("@MA_DV", SqlDbType.VarChar, 14);
            parameters[6].Value = this.MaDoanhNghiep;

            parameters[7] = new SqlParameter("@Ma_DVKB", SqlDbType.VarChar, 14);
            parameters[7].Value = this.Ma_DVKB;

            parameters[8] = new SqlParameter("@TrangThaiXL", SqlDbType.Int);
            parameters[8].Value = 0;

            parameters[9] = new SqlParameter("@AppSource", SqlDbType.NVarChar, 50);
            parameters[9].Value = this.AppSource;

            parameters[10] = new SqlParameter("@So_HSTL", SqlDbType.BigInt);
            parameters[10].Value = this.SoHoSoThanhLy;

            return SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query.ToString(), parameters);

        }

        public void LoadNPLCollection()
        {
            this.NPLCollection =BKNPLHuyBieuTang.LoadCollection(this.MaHaiQuan, this.SoTiepNhan, this.NamTiepNhan);
        }
        public void insertFull(SqlTransaction transaction)
        {
            this.Insert(transaction);
            foreach (BKNPLHuyBieuTangInfo info in this.NPLCollection)
            {
                BKNPLHuyBieuTang entity = new BKNPLHuyBieuTang();
                entity.Load(info);
                entity.SoTiepNhan = this.SoTiepNhan;
                entity.NamTiepNhan = this.NamTiepNhan;   
                entity.Insert(transaction);
            }
        }
        public void deleteFull(SqlTransaction transaction)
        {
            BKNPLHuyBieuTang.DeleteByForeignKey(transaction, this.MaHaiQuan, this.SoTiepNhan, this.NamTiepNhan);
            this.Delete(transaction);
        }
    }
}
